package com.smxy.mall.controller;

import com.smxy.mall.annotation.CurrentUser;
import com.smxy.mall.common.Response;
import com.smxy.mall.entity.Order;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.OrderReq;
import com.smxy.mall.model.request.order.OrderPageReq;
import com.smxy.mall.service.OrderService;
import com.smxy.mall.utils.Common;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @ApiOperation("订单入库")
    @PostMapping("/save")
    public Object save(@Valid OrderReq orderReq, @CurrentUser Current current) {
        return Response.success(orderService.add(orderReq,current));
    }

//    /**
//     * 订单列表
//     * @return
//     */
//    @RequestMapping("list")
//    public Object list(){
//        return Response.success(orderService.list());
//    }
//
//    /**
//     * 用户全部订单
//     * @param userId
//     * @return
//     */
//    @RequestMapping("/listAll")
//    public Object listAll(@RequestParam("userId")Integer userId){
//        return Response.success(orderService.listAll(userId));
//    }
//
//    /**
//     * 根据订单状态查询
//     * @param status
//     * @return
//     */
//    @RequestMapping("/listStatus")
//    public Object listStatus(@RequestParam("status")Integer status,@RequestParam("userId")Integer userId){
//        return Response.success(orderService.listStatus(status,userId));
//    }
//
    /**
     * 订单详情
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public Object info(@RequestParam("id")Integer id) {
        if(!Common.isId(id.toString())){
            return Response.fail("订单id异常");
        }
        return Response.success(orderService.detail(id));
    }

    @ApiOperation("当前订单")
    @GetMapping("/list/now")
    public Object listNow(@Valid OrderPageReq orderPageReq,@CurrentUser Current current){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of((orderPageReq.getPage()-1),orderPageReq.getSize(),sort);
        Page<Order> list = orderService.listNow(orderPageReq.getShopId(),current.getUserId(),pageable);
        return Response.success(list);
    }

    @ApiOperation("全部订单")
    @GetMapping("/list/all")
    public Object listAll(@Valid OrderPageReq orderPageReq,@CurrentUser Current current){
        Pageable pageable = PageRequest.of((orderPageReq.getPage()-1),orderPageReq.getSize(),Sort.by(Sort.Direction.DESC, "id"));
        return Response.success(orderService.listAll(orderPageReq.getShopId(),current.getUserId(),pageable));
    }

    @ApiOperation("订单列表根据状态")
    @GetMapping("/list/page")
    public Object listPage(@Valid OrderPageReq orderPageReq, @CurrentUser Current current){
        Pageable pageable = PageRequest.of((orderPageReq.getPage()-1),orderPageReq.getSize(),Sort.by(Sort.Direction.DESC, "id"));
        return Response.success(orderService.listPage(orderPageReq.getShopId(),current.getUserId(),orderPageReq.getStatus(),pageable));
    }
}
